package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.impl.DeptService;
import com.itheima.service.impl.EmpService;
import org.apache.ibatis.annotations.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author 郭腾高
 * @Date 2024/9/4 20:32
 */

@RestController
public class DeptController {

   // private static Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpService empService;
    //     查询部门数据
    @GetMapping("/depts")
    public Result list(){
        //log.info("查询全部部门数据");
        List<Dept> deptList = deptService.list();
        //调用DeptService的方法
        return Result.success(deptList);
    }
    //加入事务进行处理
    @Transactional
    @Log
    //删除对应id部门
    @DeleteMapping("/depts/{id}")
    public Result deleteById(@PathVariable Integer id){
        deptService.deleteById(id);
        //并删除该部门下的员工
        empService.deleteByDeptId(id);
        return Result.success();
    }
    @Log
    //新增部门
    @PostMapping("depts")
    public Result insert(@RequestBody Dept dept){
         deptService.insert(dept);
         return Result.success();
    }
}
